<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="imgbox">
        <img src="../assets/logo.png"
             alt="">
      </div>
      <!-- 表单区域 -->
      <el-form :model="form"
               ref="loginref"
               :rules="loginrules"
               label-width="0px"
               class="loginclass">
        <!-- 输入框 -->
        <el-form-item prop="username">
          <el-input v-model="form.username"
                    prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password"
                    type="password"
                    prefix-icon="el-icon-s-promotion"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btnclass">
          <el-button type="primary"
                     @click="login">登录</el-button>
          <el-button type="info"
                     @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      loginrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm: function () {
      this.$refs.loginref.resetFields()
    },
    login: function () {
      this.$refs.loginref.validate(async val => {
        if (!val) return
        const res = await this.$http.post('login', this.form)
        if (res.data.meta.status !== 200) return this.$message.error('用户名不存在')
        this.$message.success('登录成功')
        const datas = res.data
        window.sessionStorage.setItem('token', datas.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  height: 300px;
  width: 450px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.imgbox {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    height: 100%;
    width: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.loginclass {
  position: absolute;
  bottom: 0;
  padding: 0 10px;
  width: 100%;
  box-sizing: border-box;
}
.btnclass {
  display: flex;
  justify-content: flex-end;
}
</style>
